/*the most important file in this gogame app GUI
 *mostly items been operated in this obj
 *and it depends on go.h to do core-things
 *with GUI and core-things we can now
 *play gogame using these codes ;)
 */

#ifndef BOARDSCENE_H
#define BOARDSCENE_H

#include <QGraphicsScene>

#include "go.h" //core
#include "judgment/judgment.h"

class Grid;
class Stones;
class Moyos;
class QColor;
class Index;
class QTextCodec;
class QString;
class QFile;
class Settings;

class BoardScene : public QGraphicsScene
{

    Q_OBJECT

public:

    BoardScene();
    ~BoardScene(); //before delete scene, clear all the items on it, not necessary
                   //cause QT delete his objs automatically
                   //but objs not belong to QT have to be deleted manually

    /* public cause upper objs will use them */
    void createGrid();
    void deleteGrid();
    void showIndex();
    void hideIndex();

public slots:
    void changeIfShowIndex(bool);
       signals: //emit signals to upper objs
    void changeButtonToFalse();
    void openReview();
    void closeReview();
    void disableGameSettings();
    void disableShowIndex();
    void enableShowIndex();
    void showJudgment(QString);
    void windowIsModified();
    void loadSucceed();
    void loadFailed();

public:
    //slots linking to toolbutton
    void startReview();
    void backToFirst();
    void jumpBackward();
    void backward();
    void forward();
    void jumpForward();
    void goToEnd();
    void backToInit();
    void quitReview();
    void regret();
    void startJudgment();
    void pass();
    void deleteAllMoyos();
    void aiMove();

    //slots linking to the IO of game
    void outputSGF(QFile*, Settings*);
    void inputSGF(QFile*, Settings*);
    void output2DBoard(const char*);
    //slots linking to the changes of game-settings
    void changeHandicaps(int);

protected:
    void mousePressEvent(QGraphicsSceneMouseEvent *event); //main function for playing Gogame
                                                           //from a mouseclick we cast core-codes(go.h and his implements)
    void drawBackground(QPainter *painter, const QRectF &rect);

private:
    void changeYesImage(int); //yes image tells users it whos turn
    void changeSquare(int x, int y); //squre tells users where is the last step
    void changeIndexTZS(int bw, unsigned int num); //TZS tells users how many stones been taked-off by each other
    void changeIndexLableString(unsigned int); //this infoIndexLable tells how many steps been played so far
    void createInfoBlock();
    void setTextCode(); //set textcode to gbk to display some chinese but not working on friends computer ?!
    void createColor();
    void createStone(int color, int x, int y); //include create single Index
    void deleteStone(int x, int y); //include delete single Index
    void createMoyos(int color, int x, int y);
    void deleteMoyos(int x, int y);
    void createAllMoyos(); //only invoked after judgment, that means board in Judgment was overwritten
    void createIndex(int color, int x, int y, unsigned int i); //i->index
    void deleteIndex(int x, int y);
    void refreshIndex(); //clear indexOperator and recreate all index through record
    bool getPosition(QPointF point); //get the value of xpos and ypos from leftmouseclick
    void redraw(); // used to redraw the board in other public functions

    void goCore(int x, int y); //interface to connect to CoreCode Go Class
    bool inputCore(io_series*); //input game to core, load a game

private:
    bool ifShowIndex;
    int xpos,ypos; //the locations that determined to put stone
    map<coordinate, Index*> indexOperator; //管理正常状态下的手数对象
    map<coordinate, Index*> reviewIndexOperator; //管理复盘状态下的手数对象
    map<coordinate, Stones*> stonesOperator; //operate the stoneitems, for deleting
    map<coordinate, Moyos*> moyosOperator; //形势判断后的图形输出管理器
    QGraphicsRectItem *square; //always bind with the lastest Step
    Grid *grid;
    QColor *boardColor;
    QColor *infoBlockColor;
    Go *pcore; //Core Object
    Judgment *pjudgment; //Judgment Object

    /* variables for infoBlock to display */
    QGraphicsItemGroup *infoNormalGroup; //

    Index *infoIndexLable; //显示当前手数的数字字符串
    Index *infoIndexHTZS; //黑提子的数量
    Index *infoIndexBTZS; //白提子的数量
    QGraphicsSimpleTextItem *infoDQSS; //"当前手数 :"
    QGraphicsSimpleTextItem *infoH; //"黑"
    QGraphicsSimpleTextItem *infoB; //"白"
    QGraphicsSimpleTextItem *infoHYS; //"用时"
    QGraphicsSimpleTextItem *infoBYS; //"用时"
    QGraphicsSimpleTextItem *infoHTZS; //"提子数"
    QGraphicsSimpleTextItem *infoBTZS; //"提子数"
    QGraphicsPixmapItem *infoLSX; //轮到哪一方下的指示 一个钩子图片
    Stones *black;
    Stones *white;

    unsigned int info_index; //手数
    unsigned int htzs; //黑提子数
    unsigned int btzs; //白提子数
};

#endif // BOARDSCENE_H
